/*
 * 
 */
package com.c2soft.ncmxtradeX.webApp.security;

import java.io.IOException;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.log4j.Log4j2;

import org.codehaus.plexus.util.Base64;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;

@Log4j2
public class RoleBasedAuthenticationSuccessRedirectHandler extends
		SimpleUrlAuthenticationSuccessHandler {
	
	
	@Override
	public void onAuthenticationSuccess(HttpServletRequest request,
			HttpServletResponse response, Authentication authentication)
			throws ServletException, IOException {
	      
		
		  request.getSession().setAttribute("trdUsrId", request.getParameter("traderId"));
	
		  String adminTargetUrl = "/index";
		
		  String xchangeTargetUrl = "/index";
			
		Set<String> roles = AuthorityUtils.authorityListToSet(authentication
				.getAuthorities());
		if (roles.contains("ROLE_ADMIN")) {
			getRedirectStrategy().sendRedirect(request, response,
					adminTargetUrl);
		} 
		else if (roles.contains("ROLE_EXCHANGE")) {
			getRedirectStrategy()
					.sendRedirect(request, response, xchangeTargetUrl);
		}
		else {
			super.onAuthenticationSuccess(request, response, authentication);
			return;
		}
	}
}
